home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: reversing a string
- Date: Sun, 07 Apr 96 10:48:36 GMT
- Organization: none
- Message-ID: <828874116snz@genesis.demon.co.uk>
- References: <4k6cjl$j8f@central.server.swt.edu>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4k6cjl$j8f@central.server.swt.edu>
- ln16674@nyssa.swt.edu "Leland Newsom" writes:
-
- >I have a challenge from a friend of mine. He wanted me to reverse a string
- >with recursion without using any additional variables or loops. I got mine
- >to work by using exclusive or, but I needed an additional variable. Can
- >someone help with this problem without using the additional variable?
-
- There is a solution although (the one I'm thinking of) won't gain any
- prizes for efficiency. I won't give the answer (yet) because this is a
- worthwhile problem for getting used to recursion. All you need is a function:
-
-
- void reverse(char *str)
- {
- ...
- }
-
- Within that the operations you have available are to swap 2 characters at
- the start of the string (you don't know where the end is) using, say, an
- exclusive or swap, and to reverse a shorter suffix of the string via a
- recursive call. You should aim to get from something like:
-
- a b c d e f g
-
- to
-
- g a b c d e f
-
- which can then be completed via a call to reverse(str+1)
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-